﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyPhongKham
{
    public partial class ucDanhSachKham : BaseUserControl
    {
        private DAL.AccessData m;
        private string user;
        private DataSet ds = new DataSet();
        private bool bSkip = false;
        public ucDanhSachKham(DAL.AccessData acc)
        {
            this.m = acc;
            InitializeComponent();
            this.Title = "Danh sách bệnh nhân";
        }

        private void frmDanhsachkham_Load(object sender, EventArgs e)
        {
            user = m.user;
            cboPhongkham.DisplayMember = "TENKP";
            cboPhongkham.ValueMember = "MAKP";
            cboPhongkham.DataSource = m.get_data("select makp,tenkp from " + user + ".btdkp").Tables[0];
            cboBacsi.DisplayMember = "HOTEN";
            cboBacsi.ValueMember = "MA";
            cboBacsi.DataSource = m.get_data("select ma,hoten from " + user + ".dmbs").Tables[0];
            load_grid();
        }

        private void butDongy_Click(object sender, EventArgs e)
        {
            load_grid();
        }
        private void load_grid()
        {
            string sql = "";
            decimal sotien = 0;
            sql = "select f.tenkp,a.mabn,b.hoten,b.namsinh,to_char(a.ngay,'dd/mm/yyyy hh24:mi') as ngay,b.diachi,c.tentt,d.tenquan,e.tenpxa,g.hoten as tenbs,a.stt,a.trieuchung as lydo,a.done,a.xutri from " + user + ".benhanpk a," + user + ".btdbn b," + user + ".btdtt c," + user + ".btdquan d," + user + ".btdpxa e," + user + ".btdkp f," + user + ".dmbs g ";
            sql += "where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.makp=f.makp and a.mabs=g.ma and to_date(to_char(a.ngay,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('" + tungay.Text.Substring(0, 10) + "','dd/mm/yyyy') and to_date('" + denngay.Text.Substring(0, 10) + "','dd/mm/yyyy')";
            if (cboPhongkham.SelectedIndex != -1) sql += " and a.makp='" + cboPhongkham.SelectedValue.ToString() + "'";
            if (cboBacsi.SelectedIndex != -1) sql += " and a.mabs='" + cboBacsi.SelectedValue.ToString() + "'";
            ds = m.get_data(sql);
            dataGrid1.DataSource = ds.Tables[0];
            lTongso.Text = "TỔNG SỐ :" + ds.Tables[0].Rows.Count + "  (ĐÃ KHÁM : " + ds.Tables[0].Select("done=1", "").Length + " )";
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                if (r["xutri"].ToString() != "")
                {
                    sotien += decimal.Parse(r["xutri"].ToString());
                }
            }
            lTienkham.Text = "TỔNG SỐ TIỀN : " + sotien.ToString("###,###,##0");
            if (!bSkip)
            {
                AddGridTableStyle();
                bSkip = true;
            }
        }
        private void AddGridTableStyle()
        {
            DataGridColoredTextBoxColumn TextCol;
            delegateGetColorRowCol de = new delegateGetColorRowCol(MyGetColorRowCol);
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = ds.Tables[0].TableName;
            ts.AlternatingBackColor = Color.Linen;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = SystemColors.Control;
            ts.HeaderBackColor = SystemColors.Control;
            ts.HeaderForeColor = Color.Navy;
            ts.SelectionBackColor = Color.Teal;
            ts.SelectionForeColor = Color.White;
            ts.ReadOnly = false;
            ts.RowHeaderWidth = 5;

            TextCol = new DataGridColoredTextBoxColumn(de, 0);
            TextCol.MappingName = "tenkp";
            TextCol.HeaderText = "Phòng khám";
            TextCol.Width = 120;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 1);
            TextCol.MappingName = "mabn";
            TextCol.HeaderText = "Mã BN";
            TextCol.Width = 80;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 2);
            TextCol.MappingName = "hoten";
            TextCol.HeaderText = "Họ và tên";
            TextCol.Width = 200;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 3);
            TextCol.MappingName = "namsinh";
            TextCol.HeaderText = "Năm sinh";
            TextCol.Width = 120;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 4);
            TextCol.MappingName = "ngay";
            TextCol.HeaderText = "Ngày vào khám";
            TextCol.Width = 120;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 5);
            TextCol.MappingName = "tenbs";
            TextCol.HeaderText = "Bác sĩ khám";
            TextCol.Width = 160;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 6);
            TextCol.MappingName = "stt";
            TextCol.HeaderText = "Số khám";
            TextCol.Width = 70;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 7);
            TextCol.MappingName = "diachi";
            TextCol.HeaderText = "Địa chỉ nhà";
            TextCol.Width = 120;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 8);
            TextCol.MappingName = "tentt";
            TextCol.HeaderText = "Tỉnh/Thành phố";
            TextCol.Width = 100;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 9);
            TextCol.MappingName = "tenquan";
            TextCol.HeaderText = "Quận/Huyện";
            TextCol.Width = 120;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 10);
            TextCol.MappingName = "tenpxa";
            TextCol.HeaderText = "Phường/Xã";
            TextCol.Width = 120;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 11);
            TextCol.MappingName = "lydo";
            TextCol.HeaderText = "Lý do khám";
            TextCol.Width = 140;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 12);
            TextCol.MappingName = "done";
            TextCol.HeaderText = "";
            TextCol.Width = 0;
            TextCol.ReadOnly = true;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);
        }
        public Color MyGetColorRowCol(int row, int col)
        {
            Color c = Color.Navy;
            try
            {
                if (dataGrid1[row, 12].ToString() == "1")
                {
                    c = Color.Red;
                }
                if (row == dataGrid1.CurrentRowIndex)
                {
                    dataGrid1.Select(row);
                    c = Color.White;
                }
            }
            catch
            {
            }
            return c;
        }
        public delegate Color delegateGetColorRowCol(int row, int col);
        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //fix:
            //this.Close();
        }

        private void tungay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("Tab");
            }
        }

        private void butIn_Click(object sender, EventArgs e)
        {
            if (ds.Tables[0].Rows.Count == 0) return;
            ds.WriteXml(@"..\dskham.xml", XmlWriteMode.WriteSchema);
            string tenfile = "";
            if (tungay.Text.Substring(0, 10) == denngay.Text.Substring(0, 10)) tenfile = "Ngày " + tungay.Text.Substring(0, 10);
            else tenfile = "Từ ngày " + tungay.Text.Substring(0, 10) + " đến ngày " + denngay.Text.Substring(0, 10);
            frmReport f = new frmReport(m, ds.Tables[0], "", "rptDskhambenh.rpt", tenfile, "", "", "", "", "", "", "", "", "", "", "");
            f.Title = "Danh sách khám bệnh";
            this.FormMain.OpenTab(f);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cboPhongkham.SelectedIndex == -1) return;
            int idex = cboPhongkham.Items.Count - 1;
            if (cboPhongkham.SelectedIndex != idex) return; 
            if (ds.Tables[0].Rows.Count == 0) return;
            ds.WriteXml(@"..\dspttt.xml", XmlWriteMode.WriteSchema);
            string tenfile = "";
            if (tungay.Text.Substring(0, 10) == denngay.Text.Substring(0, 10)) tenfile = "Ngày " + tungay.Text.Substring(0, 10);
            else tenfile = "Từ ngày " + tungay.Text.Substring(0, 10) + " đến ngày " + denngay.Text.Substring(0, 10);
            frmReport f = new frmReport(m, ds.Tables[0], "", "rptDsthuthuat.rpt", tenfile, "", "", "", "", "", "", "", "", "", "", "");
            f.Title = "Danh sách thủ thuật";
            this.FormMain.OpenTab(f);
        }
    }
}